// 函数定义
function hello(message: string): void {
    console.log(message)
}
// => 表示返回值
type GetUserNameType = (firstName: string, lastName: string) => string;
// type用来定义一个类型或者别名
let getUserName: GetUserNameType = function (firstName: string, lastName: string): string {
    return firstName + lastName;
}
// 可选参数
function print2(name: string, age?: number, home?: string) {

}
print2('d') // 一个也不传
print2('leichao', 24)
print2('leichao', 25, '江西丰城市')
// print('leichao', 25, '江西丰城市','ddd')

// 默认参数
function ajax(url: string, method: string = 'GET') {
    console.log(url, method)
}
ajax('/user')
// 剩余参数
function sum(...numbers: Array<number>): number {
    return numbers.reduce((pre, cur) => {
        return pre + cur
    }, 0)
}
let res = sum(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
console.log(res)
// 函数重载
let obj:any = {}
// 重载的函数定义只能跟重载的函数写在一块,并且重载函数定义不用写函数体
function attr(val:string):void;
function attr(val:number):void;
function attr (val:any) :void{
    if (typeof val === 'number') {
        obj.age = val
    } else if (typeof val === 'string') {
        obj.name = val
    }
}
attr(27)
attr('雷超')
// attr(true)
console.log(obj)